package com.turnengine.client.global.game.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.global.game.bean.GameVersionSerializer;
import com.turnengine.client.global.game.bean.IGameVersion;
import java.io.IOException;

/**
 * The New Game Version Data Serializer.
 */
public class NewGameVersionDataSerializer extends AbstractCommandDataSerializer<IGameVersion> implements INewGameVersionDataSerializer {

	/**
	 * Creates a new New Game Version Data Serializer.
	 */
	public NewGameVersionDataSerializer() {
		super(NewGameVersion.COMMAND_ID);
	}

	@Override
	public IGameVersion readReturnValue(IDataReader reader) throws IOException {
		IGameVersion value = new GameVersionSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IGameVersion value) throws IOException {
		new GameVersionSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IGameVersion> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		String gameName = reader.readString(true);
		String versionName = reader.readString(true);

		// Create object
		NewGameVersion object = new NewGameVersion();
		object.setLoginId(loginId);
		object.setGameName(gameName);
		object.setVersionName(versionName);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IGameVersion> argument) throws IOException {
		NewGameVersion object = (NewGameVersion) argument;

		// Get fields
		long loginId = object.getLoginId();
		String gameName = object.getGameName();
		String versionName = object.getVersionName();

		// Write fields
		writer.writeLong(loginId);
		writer.writeString(gameName, true);
		writer.writeString(versionName, true);
	}

	@Override
	public IExecutableCommandResponse<IGameVersion> newResponse() {
		return new NewGameVersionResponse();
	}
}
